package com.global.college.ui.presenter;

import android.content.Context;
import android.util.Log;

import com.aliyun.player.alivcplayerexpand.constants.GlobalPlayerConfig;
import com.aliyun.player.alivcplayerexpand.widget.AliyunVodPlayerView;
import com.aliyun.player.nativeclass.CacheConfig;
import com.aliyun.player.nativeclass.PlayerConfig;
import com.aliyun.svideo.common.utils.FileUtils;
import com.global.college.R;
import com.global.college.api.ProgressSubscriber;
import com.global.college.base.BasePresenter;
import com.global.college.data.net.CourseDetailBean;
import com.global.college.data.net.VideoDetailBean;
import com.global.college.data.net.VideoHistoryBean;
import com.global.college.ui.interfaces.ICourseDetailView;


public class CourseDetailPresenter extends BasePresenter {

    private ICourseDetailView iCourseDetailView;

    public CourseDetailPresenter(Context context) {
        super(context);
    }

    public void setISelectView(ICourseDetailView iDownloadView) {
        iCourseDetailView = iDownloadView;
    }

    /**
     * @param videoId
     */
    public void getVideoDetail(String videoId) {
        mRequestClient.getVideoDetail(videoId).subscribe(new ProgressSubscriber<VideoDetailBean>(mContext, false) {
            @Override
            public void onNext(VideoDetailBean model) {
                if (null != iCourseDetailView) {
                    iCourseDetailView.onGetVideoDetail(model);
                }
            }
        });
    }


    /**
     * 免费商品直接调用创建订单接口，无需后续逻辑
     */
    public void createOrder(String userId, String goodId) {
        mRequestClient.createOrder(userId, goodId).subscribe(new ProgressSubscriber<Object>(mContext, true) {
            @Override
            public void onNext(Object object) {
            }
        });
    }

    /**
     * 记录播放历史
     *
     * @param userId
     * @param videoId
     * @param trainId
     * @param classId
     * @param duration
     */
    public void saveVideoHistory(String userId, String videoId, String trainId, String classId, int duration) {
        mRequestClient.saveVideoHistory(userId, videoId, trainId, classId, duration).subscribe(new ProgressSubscriber<Object>(mContext, false) {
            @Override
            public void onNext(Object model) {

            }

        });
    }

    /**
     * 获取播放历史
     *
     * @param userId
     * @param trainId
     * @param classId
     */
    public void getVideoHistory(String userId, String trainId, String classId) {
        mRequestClient.getVideoHistory(userId, trainId, classId).subscribe(new ProgressSubscriber<VideoHistoryBean>(mContext, true) {
            @Override
            public void onNext(VideoHistoryBean model) {
                if (null != iCourseDetailView) {
                    iCourseDetailView.onGetVideoHistory(model);
                }
            }
        });
    };

    /**
     * 获取课程详情
     *
     * @param goodId
     */
    public void getGoodsDetail(String goodId) {
        mRequestClient.getCourseDetail(goodId).subscribe(new ProgressSubscriber<CourseDetailBean>(mContext, false) {
            @Override
            public void onNext(CourseDetailBean model) {
                if (null != iCourseDetailView) {
                    //接口问题 把内层数据取到外层
                    model.setClassCover(model.getClassModel().getClassCover());
                    model.setClassNote(model.getClassModel().getClassNote());
                    iCourseDetailView.onGetCourseDetail(model);
                }
            }
        });
    }

    /**
     * 获取我的课程详情
     * @param classId
     */
    public void getMyCourseDetail(String classId) {
        mRequestClient.getMyCourseDetail(classId).subscribe(new ProgressSubscriber<CourseDetailBean>(mContext, false) {
            @Override
            public void onNext(CourseDetailBean model) {
                if (null != iCourseDetailView) {
                    model.setGoodsBuy(1d);
                    iCourseDetailView.onGetCourseDetail(model);
                }
            }
        });
    }

    public void initPlayerConfig(AliyunVodPlayerView mAliyunVodPlayerView) {
        if (mAliyunVodPlayerView != null) {
            //界面设置
            mAliyunVodPlayerView.setEnableHardwareDecoder(GlobalPlayerConfig.mEnableHardDecodeType);
            mAliyunVodPlayerView.setRenderMirrorMode(GlobalPlayerConfig.mMirrorMode);
            mAliyunVodPlayerView.setRenderRotate(GlobalPlayerConfig.mRotateMode);
            //播放配置设置
            PlayerConfig playerConfig = mAliyunVodPlayerView.getPlayerConfig();
            playerConfig.mStartBufferDuration = GlobalPlayerConfig.PlayConfig.mStartBufferDuration;
            playerConfig.mHighBufferDuration = GlobalPlayerConfig.PlayConfig.mHighBufferDuration;
            playerConfig.mMaxBufferDuration = GlobalPlayerConfig.PlayConfig.mMaxBufferDuration;
            playerConfig.mMaxDelayTime = GlobalPlayerConfig.PlayConfig.mMaxDelayTime;
            playerConfig.mNetworkTimeout = GlobalPlayerConfig.PlayConfig.mNetworkTimeout;
            playerConfig.mMaxProbeSize = GlobalPlayerConfig.PlayConfig.mMaxProbeSize;
            playerConfig.mReferrer = GlobalPlayerConfig.PlayConfig.mReferrer;
            playerConfig.mHttpProxy = GlobalPlayerConfig.PlayConfig.mHttpProxy;
            playerConfig.mNetworkRetryCount = GlobalPlayerConfig.PlayConfig.mNetworkRetryCount;
            playerConfig.mEnableSEI = GlobalPlayerConfig.PlayConfig.mEnableSei;
            playerConfig.mClearFrameWhenStop = GlobalPlayerConfig.PlayConfig.mEnableClearWhenStop;
            mAliyunVodPlayerView.setPlayerConfig(playerConfig);
            //缓存设置
            initCacheConfig(mAliyunVodPlayerView);
            Log.e(TAG, "cache dir : " + GlobalPlayerConfig.PlayCacheConfig.mDir
                    + " startBufferDuration = " + GlobalPlayerConfig.PlayConfig.mStartBufferDuration
                    + " highBufferDuration = " + GlobalPlayerConfig.PlayConfig.mHighBufferDuration
                    + " maxBufferDuration = " + GlobalPlayerConfig.PlayConfig.mMaxBufferDuration
                    + " maxDelayTime = " + GlobalPlayerConfig.PlayConfig.mMaxDelayTime
                    + " enableCache = " + GlobalPlayerConfig.PlayCacheConfig.mEnableCache
                    + " --- mMaxDurationS = " + GlobalPlayerConfig.PlayCacheConfig.mMaxDurationS
                    + " --- mMaxSizeMB = " + GlobalPlayerConfig.PlayCacheConfig.mMaxSizeMB);
        }
    }

    private void initCacheConfig(AliyunVodPlayerView mAliyunVodPlayerView) {
        CacheConfig cacheConfig = new CacheConfig();
        GlobalPlayerConfig.PlayCacheConfig.mDir = FileUtils.getDir(mContext) + GlobalPlayerConfig.CACHE_DIR_PATH;
        cacheConfig.mEnable = GlobalPlayerConfig.PlayCacheConfig.mEnableCache;
        cacheConfig.mDir = GlobalPlayerConfig.PlayCacheConfig.mDir;
        cacheConfig.mMaxDurationS = GlobalPlayerConfig.PlayCacheConfig.mMaxDurationS;
        cacheConfig.mMaxSizeMB = GlobalPlayerConfig.PlayCacheConfig.mMaxSizeMB;

        mAliyunVodPlayerView.setCacheConfig(cacheConfig);
    }

}
